*November 16, 2025
**Wilk, Deza, Hodge, Danagoulian (2025) Couch-Locked with the Munchies: Effects of Recreational Marijuana Laws on Exercise and Nutrition


* TIME PERIODS: Cannabis: 2011-2020, weekly and monthly (match BRFSS data best; 2011-2021)

* NOTES: PRODUCTS flow into PURCHASES, PURCHASES flow into TRIPS, TRIPS flow into PANELIST via Household
* Start with all 6M+ UPCs
import delimited using "products.tsv", delimiter("\t") clear
save "products.dta", replace

***** Import annual .tsv files *****
* Purchases, Trips, and Panelist (and products_extra just in case)
forvalues i=2011/2020 {
	local type trips purchases panelists products_extra
	foreach x of local type {
		import delimited using "HMS/`i'/Annual_Files/`x'_`i'.tsv", delimiter("\t") clear
		save "`x'_`i'.dta", replace
	} // type loop
} // year loop

***** CANNABIS Related Product Identificcation *****
use "products.dta", clear
* Cookies and Ice Cream
gen snacks = 0
replace snacks = 1 if strpos(product_module_descr, "SNACK")
gen cookies = 0
replace cookies = 1 if product_group_descr == "COOKIES"
gen chips = 0
replace chips = 1 if strpos(product_module_descr, "CHIPS") & product_group_descr == "SNACKS"
gen candy = 0
replace candy = 1 if product_group_descr == "CANDY"
gen icecream = 0
replace icecream = 1 if strpos(product_module_descr, "ICE CREAM - BULK")
replace icecream = 1 if strpos(product_module_descr, "MIXES-ICE CREAM")
* Indicator of all key product types
gen key = snacks + cookies + chips + icecream
drop if key == 0
gen upc_s = string(upc, "%012.0f")
gen v = string(upc_ver_uc)
gen vupc = v + upc_s
replace size1_units = subinstr(size1_units," ","",.)
save "munchy_products_new.dta", replace

* bring in each file for each year
forvalues i=2011/2020 {
	use "purchases_`i'.dta"
	gen upc_s = string(upc, "%012.0f")
	gen v = string(upc_ver_uc)
	gen vupc = v + upc_s
	merge m:1 vupc using "munchy_products_new.dta", keep(master matched) nogen
	merge m:1 trip_code_uc using "trips_`i'.dta", keep(master matched) nogen
	rename household_code household_cd
	merge m:1 household_cd using "panelists_`i'.dta", keep(master matched) nogen
	save "consumer_panel_`i'_new.dta", replace
	clear all
} // merge loop
use "Loops/consumer_panel_2011_new.dta", clear
forvalues i=2012/2020 {
	append using "Loops/consumer_panel_`i'_new.dta"
} // append loop
save "consumer_panel_new.dta", replace
* Condense to week-year, household, and category levels
* dates start on Sunday and end on Saturday; 523 weeks total
gen date = date(purchase_date, "YMD")
gen wy = 1 + floor((date - 18622)/7)
gen wys = string(wy, "%03.0f")
gen date_week = 18622 + 7*(wy - 1)
format date date_week %td

gen st = string(fips_state_cd, "%02.0f")
gen cn = string(fips_county_cd, "%03.0f")
gen yr = string(panel_year, "%04.0f")
gen fips = st + cn
gen fpwy = fips + wys
gen fy  = fips + yr
destring fips fpwy fy, replace force

drop upc upc_ver_uc coupon_value deal_flag_uc upc_s v vupc upc_descr product_module_code product_module_descr product_group_code product_group_descr department_code department_descr brand_code_uc brand_descr malt porter retailer_code store_code_uc store_zip3 projection_factor_magnet fips_state_desc fips_county_desc region_cd scantrack_market_identifier_cd scantrack_market_identifier_desc dma_cd dma_name tv_items household_internet_connection wic_indicator_current wic_indicator_ever_not_current

sort trip_code_uc
egen total_trip_spent = sum(total_price_paid), by(trip_code_uc)

local trips junk snacks chips cookies candy icecream
foreach x of local trips {
	egen `x'_trip = max(`x'), by(trip_code_uc)
} // trip loop

local cat junk snacks chips cookies candy icecream
foreach x of local cat {
	gen `x'_paid_temp = total_price_paid if `x'==1
	egen `x'_paid = sum(`x'_paid_temp), by(trip_code_uc)
} // category loop

local agro total_trip_spent junk_paid snacks_paid chips_paid cookies_paid candy_paid icecream_paid junk_trip snacks_trip chips_trip cookies_trip candy_trip icecream_trip
foreach x of local agro {
	bysort trip_code_uc: replace `x' = . if _n!=1
} // agro loop
gen total_trips = 1 if total_trip_spent!=.

* only measures for junk foods
gen temp_oz = 0
replace temp_oz = size1_amount if size1_unit=="OZ"
gen oz = temp_oz * multi
gen temp_cnt = 0
replace temp_cnt = size1_amount if size1_unit=="CT"
gen cnt = temp_cnt * multi

local quant junk snacks chips cookies candy icecream
foreach x of local quant {
	gen q`x' = 0
	replace q`x' = quantity if `x'==1
} // quantity loop

local quant junk snacks chips cookies candy icecream
foreach x of local quant {
	gen amt_`x'_oz = 0
	replace amt_`x'_oz = q`x'*oz if `x'==1 & oz!=0
	gen amt_`x'_cnt = 0
	replace amt_`x'_cnt = q`x'*cnt if `x'==1 & cnt!=0
} // amount loop
gen amt_oz = 0
replace amt_oz = oz*quantity
gen amt_cnt = 0
replace amt_cnt = cnt*quantity

save "consumer_panel.dta", replace


gcollapse (sum) total_trip_spent amt_oz amt_cnt junk_paid amt_junk_oz amt_junk_cnt snacks_paid amt_snacks_oz amt_snacks_cnt chips_paid amt_chips_oz amt_chips_cnt cookies_paid amt_cookies_oz amt_cookies_cnt candy_paid amt_candy_oz amt_candy_cnt icecream_paid amt_icecream_oz amt_icecream_cnt total_trips junk_trip snacks_trip chips_trip cookies_trip candy_trip icecream_trip (mean) projection_factor household_income-panelist_zipcd kitchen_appliances-member_7_employment fips fpwy fy date_week, by(wy household_cd)

* create natural log of paid and trip variables
local paid total_trip_spent amt_oz amt_cnt junk_paid amt_junk_oz amt_junk_cnt snacks_paid amt_snacks_oz amt_snacks_cnt chips_paid amt_chips_oz amt_chips_cnt cookies_paid amt_cookies_oz amt_cookies_cnt candy_paid amt_candy_oz amt_candy_cnt icecream_paid amt_icecream_oz amt_icecream_cnt total_trips junk_trip snacks_trip chips_trip cookies_trip candy_trip icecream_trip 
foreach x of local paid {
	gen ln`x' = ln(`x' + 1)
} // ln loop
local trip junk snacks chips cookies candy icecream
foreach x of local trip {
	gen `x'_t = 0
	replace `x'_t = 1 if `x'_trip>0
} // trip loop

gen st = floor(fips/1000)

* create start and post_start for MML and RML laws for each state
gen start = 0
gen post_start = 0
* Alabama (AL | 01)
*replace start = 1 if st == 1
*replace post_start = 1 if st == 1
* Alaska (AK | 02)
	* missing
* Arizona (AZ | 04) (wy 102 | ...)
replace start = 1 if date_week >= td(06dec2012) & date_week < td(22jan2021) & st == 4
replace post_start = 1 if date_week >= td(22jan2021) & st == 4
replace start = 1 if wy==102 & st==4
* Arkansas (AR | 05) (wy 437 | ...)
replace start = 1 if date_week >= td(11may2019) & st == 5
*replace post_start = 1 if st == 5
replace start = 1 if wy==437 & st==5
* California (CA | 06) (wy   1 | 367)
replace start = 1 if date_week < td(01jan2018) & st == 6
replace post_start = 1 if date_week >= td(01jan2018) & st == 6
replace start = 0 if wy==367 & st==6
replace post_start = 1 if wy==367 & st==6
* Colorado (CO | 08) (wy  42 | 158)
replace start = 1 if date_week >= td(10oct2011) & date_week < td(01jan2014) & st == 8
replace post_start = 1 if date_week >= td(01jan2014) & st == 8
replace start = 1 if wy==42 & st==8
replace start = 0 if wy==158 & st==8
replace post_start = 1 if wy==158 & st==8
* Connecticut (CT | 09) (wy 191 | ...)
replace start = 1 if date_week >= td(20aug2014) & date_week < td(10jan2023) & st == 9
replace post_start = 1 if date_week >= td(10jan2023) & st == 9
replace start = 1 if wy==191 & st==9
* Delaware (DE | 10) (wy 235 | ...)
replace start = 1 if date_week >= td(26jun2015) & date_week < td(01aug2025) & st == 10
replace post_start = 1 if date_week >= td(01aug2025) & st == 10
replace start = 1 if wy==235 & st==10
* District of Columbia (DC | 11) (wy 136 | 218)
replace start = 1 if date_week >= td(30jul2013) & date_week < td(26feb2015) & st == 11
replace post_start = 1 if date_week >= td(26feb2015) & st == 11
replace start = 1 if wy==136 & st==11
replace start = 0 if wy==218 & st==11
replace post_start = 1 if wy==218 & st==11
* Florida (FL | 12) (wy 292 | ...)
replace start = 1 if date_week >= td(26jul2016) & st == 12
*replace post_start = 1 if st == 12
replace start = 1 if wy==292 & st==12
* Georgia (GA | 13)
	* NONE *
* Hawaii (HI | 15)
	* missing
* Idaho (ID | 16)
	* NONE *
* Illinois (IL | 17) (wy 255 | 471)
replace start = 1 if date_week >= td(09nov2015) & date_week < td(01jan2020) & st == 17
replace post_start = 1 if date_week >= td(01jan2020) & st == 17
replace start = 1 if wy==255 & st==17
replace start = 0 if wy==471 & st==17
replace post_start = 1 if wy==471 & st==17
* Indiana (IN | 18)
	* NONE *
* Iowa (IA | 19)
	* NONE *
* Kansas (KS | 20)
	* NONE *
* Kentucky (KY | 21)
	* NONE *
* Louisiana (LA | 22) (wy 450 | ...)
replace start = 1 if date_week >= td(06aug2019) & st == 22
replace start = 1 if wy==450 & st==22
*replace post_start = 1 if st == 22
* Maine (ME | 23) (wy  10 | 511)
replace start = 1 if date_week >= td(01mar2011) & date_week < td(09oct2020) & st == 23
replace post_start = 1 if date_week >= td(09oct2020) & st == 23
replace start = 1 if wy==10 & st==23
replace start = 0 if wy==511 & st==23
replace post_start = 1 if wy==511 & st==23
* Maryland (MD | 24) (wy 362 | ...)
replace start = 1 if date_week >= td(01dec2017) & date_week < td(01jul2023) & st == 24
replace post_start = 1 if date_week >= td(01jul2023) & st == 24
replace start = 1 if wy==362 & st==24
* Massachuesetts (MA | 25) (wy 235 | 413)
replace start = 1 if date_week >= td(24jun2015) & date_week < td(20nov2018) & st == 25
replace post_start = 1 if date_week >= td(20nov2018) & st == 25
replace start = 1 if wy==235 & st==25
replace start = 0 if wy==413 & st==25
replace post_start = 1 if wy==413 & st==25
* Michigan (MI | 26) (wy 397 | 467)
replace start = 1 if date_week >= td(01aug2018) & date_week < td(01dec2019) & st == 26
replace post_start = 1 if date_week >= td(01dec2019) & st == 26
replace start = 1 if wy==397 & st==26
* RML policy for MI starts exactly on start of week
* Minnesota (MN | 27) (wy 236 | ...)
replace start = 1 if date_week >= td(01jul2015) & st == 27
*replace post_start = 1 if st == 27
replace start = 1 if wy==236 & st==27
* Mississippi (MS | 28) (wy ... | ...)
replace start = 1 if date_week >= td(25jan2023) & st == 28
*replace post_start = 1 if st == 28
*replace start = 1 if wy==
* Missouri (MO | 29) (wy 512 | ...)
replace start = 1 if date_week >= td(17oct2020) & date_week < td(03feb2023) & st == 29
replace post_start = 1 if date_week >= td(03feb2023) & st == 29
replace start = 1 if wy==512 & st==29
* Montana (MT | 30) (wy 380 | ...)
replace start = 1 if date_week >= td(01apr2018) & date_week < td(01jan2022) & st == 30
replace post_start = 1 if date_week >= td(01jan2022) & st == 30
* MML policy for MT starts exactly on start of week
* Nebraska (NE | 31)
	* NONE *
* Nevada (NV | 32) (wy 240 | 340)
replace start = 1 if date_week >= td(31jul2015) & date_week < td(01jul2017) & st == 32
replace post_start = 1 if date_week >= td(01jul2017) & st == 32
replace start = 1 if wy==240 & st==32
replace start = 0 if wy==340 & st==32
replace post_start = 1 if wy==340 & st==32
* New Hampshire (NH | 33) (wy 280 | ...)
replace start = 1 if date_week >= td(01may2016) & st == 33
*replace post_start = 1 if st == 33
* MML policy for NH starts exactly on start of week
* New Jersey (NJ | 34) (wy  54 | ...)
replace start = 1 if date_week >= td(01jan2012) & date_week < td(20apr2022) & st == 34
replace post_start = 1 if date_week >= td(20apr2022) & st == 34
* MML policy for NJ starts exactly on start of week
* New Mexico (NM | 35) (wy   1 | ...)
replace start = 1 if date_week >= td(01jan2010) & date_week < td(01apr2022) & st == 35
replace post_start = 1 if date_week >= td(01apr2022) & st == 35
* New York (NY | 36) (wy 263 | ...)
replace start = 1 if date_week >= td(07jan2016) & date_week < td(29dec2022) & st == 36
replace post_start = 1 if date_week >= td(29dec2022) & st == 36
replace start = 1 if wy==263 & st==36
* North Carolina (NC | 37)
	* NONE *
* North Dakota (ND | 38) (wy 427 | ...)
replace start = 1 if date_week >= td(01mar2019) & st == 38
*replace post_start = 1 if st == 38
replace start = 1 if wy==427 & st==38
* Ohio (OH | 39) (wy 421 | ...)
replace start = 1 if date_week >= td(16jan2019) & st == 39
*replace post_start = 1 if st == 39
replace start = 1 if wy==421 & st==39
* Oklahoma (OK | 40) (wy 409 | ...)
replace start = 1 if date_week >= td(26oct2018) & st == 40
*replace post_start = 1 if st == 40
replace start = 1 if wy==409 & st==40
* Oregon (OR | 41) (wy 169 | 249)
replace start = 1 if date_week >= td(21mar2014) & date_week < td(01oct2015) & st == 41
replace post_start = 1 if date_week >= td(01oct2015) & st == 41
replace start = 1 if wy==169 & st==41
replace start = 0 if wy==249 & st==41
replace post_start = 1 if wy==249 & st==41
* Pennsylvania (PA | 42) (wy 373 | ...)
replace start = 1 if date_week >= td(15feb2018) & st == 42
*replace post_start = 1 if st == 42
replace start = 1 if wy==373 & st==42
* Rhode Island (RI | 44) (wy 106 | ...)
replace start = 1 if date_week >= td(01jan2013) & date_week < td(01dec2022) & st == 44
replace post_start = 1 if date_week >= td(01dec2022) & st == 44
replace start = 1 if wy==106 & st==44
* South Carolina (SC | 45)
	* NONE *
* South Dakota (SD | 46)
replace start = 1 if date_week >= td(27jul2022) & st == 46
*replace post_start = 1 if st == 46
* Tennessee (TN | 47)
	* NONE *
* Texas (TX | 48)
	* NONE *
* Utah (UT | 49) (wy 480 | ...)
replace start = 1 if date_week >= td(02mar2020) & st == 49
*replace post_start = 1 if st == 49
replace start = 1 if wy==480 & st==49
* Vermont (VT | 50) (wy 131 | ...)
replace start = 1 if date_week >= td(25jun2013) & date_week < td(01oct2022) & st == 50
replace post_start = 1 if date_week >= td(01oct2022) & st == 50
replace start = 1 if wy==131 & st==50
* Virginia (VA | 51) (wy 512 | ...)
replace start = 1 if date_week >= td(17oct2020) & st == 51
*replace post_start = 1 if st == 51
replace start = 1 if wy==512 & st==51
* Washington (WA | 53) (wy 185 | 185)
replace start = 1 if date_week >= td(08jul2014) & date_week < td(08jul2014) & st == 53
replace post_start = 1 if date_week >= td(08jul2014) & st == 53
replace post_start = 1 if wy==185 & st==53
* West Virginia (WV | 54) (wy ... | ...)
replace start = 1 if date_week >= td(12nov2021) & st == 54
*replace post_start = 1 if st == 54
* Wisconsin (WI | 55)
	* NONE *
* Wyoming (WY | 56)
	* NONE *

* RML state groups (by week)
gen g_id = 0
replace g_id = 367 if st == 6
replace g_id = 158 if st == 8
replace g_id = 218 if st == 11
replace g_id = 471 if st == 17
replace g_id = 511 if st == 23
replace g_id = 413 if st == 25
replace g_id = 467 if st == 26
replace g_id = 340 if st == 32
replace g_id = 249 if st == 41
replace g_id = 185 if st == 53

gen Ei = g_id
replace Ei = . if Ei == 0
gen period = 0
replace period = wy - g_id if g_id > 0
gen treat = 0
replace treat = 1 if g_id > 0
gen did = treat*post_start

* create covid indicator (anything post March 1 2020)
gen covid = 0
replace covid = 1 if wy >= 480

* create stay-at-home (shelter) indicator
gen shelter = 0
* Alabama (AL | 01) (wy 484)
replace shelter = 1 if date >= td(04apr2020) & date < td(30apr2020) & st == 1
replace shelter = 1 if wy==484 & st==1
* Arizona (AZ | 04) (wy 484)
replace shelter = 1 if date >= td(31mar2020) & date < td(16may2020) & st == 4
replace shelter = 1 if wy==484 & st==4
* Arkansas (AR | 05)
	* NONE *
* California (CA | 06) (wy 482)
replace shelter = 1 if date >= td(19mar2020) & date < td(25jan2021) & st == 6
replace shelter = 1 if wy==482 & st==6
* Colorado (CO | 08) (wy 483)
replace shelter = 1 if date >= td(26mar2020) & date < td(27apr2020) & st == 8
replace shelter = 1 if wy==483 & st==8
* Connecticut (CT | 09) (wy 483)
replace shelter = 1 if date >= td(23mar2020) & date < td(20may2020) & st == 9
replace shelter = 1 if wy==483 & st==9
* Delaware (DE | 10) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(01jun2020) & st == 10
replace shelter = 1 if wy==483 & st==10
* District of Columbia (DC | 11) (wy 484)
replace shelter = 1 if date >= td(01apr2020) & date < td(29may2020) & st == 11
replace shelter = 1 if wy==484 & st==11
* Florida (FL | 12) (wy 484)
replace shelter = 1 if date >= td(03apr2020) & date < td(18may2020) & st == 12
replace shelter = 1 if wy==484 & st==12
* Georgia (GA | 13) (wy 484)
replace shelter = 1 if date >= td(03apr2020) & date < td(01may2020) & st == 13
replace shelter = 1 if wy==484 & st==13
* Idaho (ID | 16) (wy 483)
replace shelter = 1 if date >= td(25mar2020) & date < td(01may2020) & st == 16
replace shelter = 1 if wy==483 & st==16
* Illinois (IL | 17) (wy 482)
replace shelter = 1 if date >= td(21mar2020) & date < td(29may2020) & st == 17
replace shelter = 1 if wy==482 & st==17
* Indiana (IN | 18) (wy 483)
replace shelter = 1 if date >= td(25mar2020) & date < td(18may2020) & st == 18
replace shelter = 1 if wy==483 & st==18
* Iowa (IA | 19)
	* NONE *
* Kansas (KS | 20) (wy 484)
replace shelter = 1 if date >= td(30mar2020) & date < td(04may2020) & st == 20
replace shelter = 1 if wy==484 & st==20
* Kentucky (KY | 21) (wy 483)
replace shelter = 1 if date >= td(26mar2020) & date < td(29jun2020) & st == 21
replace shelter = 1 if wy==483 & st==21
* Louisiana (LA | 22) (wy 483)
replace shelter = 1 if date >= td(23mar2020) & date < td(15may2020) & st == 22
replace shelter = 1 if wy==483 & st==22
* Maine (ME | 23) (wy 484)
replace shelter = 1 if date >= td(02apr2020) & date < td(31may2020) & st == 23
replace shelter = 1 if wy==484 & st==23
* Maryland (MD | 24) (wy 484)
replace shelter = 1 if date >= td(30mar2020) & date < td(15may2020) & st == 24
replace shelter = 1 if wy==484 & st==24
* Massachuesetts (MA | 25) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(18may2020) & st == 25
replace shelter = 1 if wy==483 & st==25
* Michigan (MI | 26) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(01jun2020) & st == 26
replace shelter = 1 if wy==483 & st==26
* Minnesota (MN | 27) (wy 483)
replace shelter = 1 if date >= td(28mar2020) & date < td(18may2020) & st == 27
replace shelter = 1 if wy==483 & st==27
* Mississippi (MS | 28) (wy 484)
replace shelter = 1 if date >= td(03apr2020) & date < td(27apr2020) & st == 28
replace shelter = 1 if wy==484 & st==28
* Missouri (MO | 29) (wy 485)
replace shelter = 1 if date >= td(06apr2020) & date < td(04may2020) & st == 29
replace shelter = 1 if wy==485 & st==29
* Montana (MT | 30) (wy 483)
replace shelter = 1 if date >= td(28mar2020) & date < td(26apr2020) & st == 30
replace shelter = 1 if wy==483 & st==30
* Nebraska (NE | 31)
	* NONE *
* Nevada (NV | 32) (wy 484)
replace shelter = 1 if date >= td(31mar2020) & date < td(09may2020) & st == 32
replace shelter = 1 if wy==484 & st==32
* New Hampshire (NH | 33) (wy 483)
replace shelter = 1 if date >= td(28mar2020) & date < td(16jun2020) & st == 33
replace shelter = 1 if wy==483 & st==33
* New Jersey (NJ | 34) (wy 482)
replace shelter = 1 if date >= td(21mar2020) & date < td(09jun2020) & st == 34
replace shelter = 1 if wy==482 & st==34
* New Mexico (NM | 35) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(30nov2020) & st == 35
replace shelter = 1 if wy==483 & st==35
* New York (NY | 36) (wy 483)
replace shelter = 1 if date >= td(22mar2020) & date < td(27jun2020) & st == 36
* NY exact start of the week
* North Carolina (NC | 37) (wy 484)
replace shelter = 1 if date >= td(30mar2020) & date < td(22may2020) & st == 37
replace shelter = 1 if wy==484 & st==37
* North Dakota (ND | 38)
	* NONE *
* Ohio (OH | 39) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(20may2020) & st == 39
replace shelter = 1 if wy==483 & st==39
* Oklahoma (OK | 40) (wy 484)
replace shelter = 1 if date >= td(01apr2020) & date < td(15may2020) & st == 40
replace shelter = 1 if wy==484 & st==40
* Oregon (OR | 41) (wy 483)
replace shelter = 1 if date >= td(23mar2020) & date < td(19jun2020) & st == 41
replace shelter = 1 if wy==483 & st==41
* Pennsylvania (PA | 42) (wy 484)
replace shelter = 1 if date >= td(01apr2020) & date < td(05jun2020) & st == 42
replace shelter = 1 if wy==484 & st==42
* Rhode Island (RI | 44) (wy 483)
replace shelter = 1 if date >= td(28mar2020) & date < td(09may2020) & st == 44
replace shelter = 1 if wy==483 & st==44
* South Carolina (SC | 45) (wy 485)
replace shelter = 1 if date >= td(07apr2020) & date < td(04may2020) & st == 45
replace shelter = 1 if wy==485 & st==45
* South Dakota (SD | 46)
	* NONE *
* Tennessee (TN | 47) (wy 484)
replace shelter = 1 if date >= td(02apr2020) & date < td(01may2020) & st == 47
replace shelter = 1 if wy==484 & st==47
* Texas (TX | 48) (wy 484)
replace shelter = 1 if date >= td(02apr2020) & date < td(01may2020) & st == 48
replace shelter = 1 if wy==484 & st==48
* Utah (UT | 49)
	* NONE *
* Vermont (VT | 50) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(15may2020) & st == 50
replace shelter = 1 if wy==483 & st==50
* Virginia (VA | 51) (wy 484)
replace shelter = 1 if date >= td(30mar2020) & date < td(29may2020) & st == 51
replace shelter = 1 if wy==484 & st==51
* Washington (WA | 53) (wy 483)
replace shelter = 1 if date >= td(23mar2020) & date < td(01jun2020) & st == 53
replace shelter = 1 if wy==483 & st==53
* West Virginia (WV | 54) (wy 483)
replace shelter = 1 if date >= td(24mar2020) & date < td(05may2020) & st == 54
replace shelter = 1 if wy==483 & st==54
* Wisconsin (WI | 55) (wy 483)
replace shelter = 1 if date >= td(25mar2020) & date < td(13may2020) & st == 55
replace shelter = 1 if wy==483 & st==55
* Wyoming (WY | 56)
	* NONE *

* create stay-at-home (shelter2) indicator that continues through 2020
gen shelter2 = 0
* Alabama (AL | 01) (wy 484)
replace shelter2 = 1 if date >= td(04apr2020) & st == 1
replace shelter2 = 1 if wy==484 & st==1
* Arizona (AZ | 04) (wy 484)
replace shelter2 = 1 if date >= td(31mar2020) & st == 4
replace shelter2 = 1 if wy==484 & st==4
* California (CA | 06) (wy 482)
replace shelter2 = 1 if date >= td(19mar2020) & st == 6
replace shelter2 = 1 if wy==482 & st==6
* Colorado (CO | 08) (wy 483)
replace shelter2 = 1 if date >= td(26mar2020) & st == 8
replace shelter2 = 1 if wy==483 & st==8
* Connecticut (CT | 09) (wy 483)
replace shelter2 = 1 if date >= td(23mar2020) & st == 9
replace shelter2 = 1 if wy==483 & st==9
* Delaware (DE | 10) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 10
replace shelter2 = 1 if wy==483 & st==10
* District of Columbia (DC | 11) (wy 484)
replace shelter2 = 1 if date >= td(01apr2020) & st == 11
replace shelter2 = 1 if wy==484 & st==11
* Florida (FL | 12) (wy 484)
replace shelter2 = 1 if date >= td(03apr2020) & st == 12
replace shelter2 = 1 if wy==484 & st==12
* Georgia (GA | 13) (wy 484)
replace shelter2 = 1 if date >= td(03apr2020) & st == 13
replace shelter2 = 1 if wy==484 & st==13
* Idaho (ID | 16) (wy 483)
replace shelter2 = 1 if date >= td(25mar2020) & st == 16
replace shelter2 = 1 if wy==483 & st==16
* Illinois (IL | 17) (wy 482)
replace shelter2 = 1 if date >= td(21mar2020) & st == 17
replace shelter2 = 1 if wy==482 & st==17
* Indiana (IN | 18) (wy 483)
replace shelter2 = 1 if date >= td(25mar2020) & st == 18
replace shelter2 = 1 if wy==483 & st==18
* Kansas (KS | 20) (wy 484)
replace shelter2 = 1 if date >= td(30mar2020) & st == 20
replace shelter2 = 1 if wy==484 & st==20
* Kentucky (KY | 21) (wy 483)
replace shelter2 = 1 if date >= td(26mar2020) & st == 21
replace shelter2 = 1 if wy==483 & st==21
* Louisiana (LA | 22) (wy 483)
replace shelter2 = 1 if date >= td(23mar2020) & st == 22
replace shelter2 = 1 if wy==483 & st==22
* Maine (ME | 23) (wy 484)
replace shelter2 = 1 if date >= td(02apr2020) & st == 23
replace shelter2 = 1 if wy==484 & st==23
* Maryland (MD | 24) (wy 484)
replace shelter2 = 1 if date >= td(30mar2020) & st == 24
replace shelter2 = 1 if wy==484 & st==24
* Massachuesetts (MA | 25) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 25
replace shelter2 = 1 if wy==483 & st==25
* Michigan (MI | 26) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 26
replace shelter2 = 1 if wy==483 & st==26
* Minnesota (MN | 27) (wy 483)
replace shelter2 = 1 if date >= td(28mar2020) & st == 27
replace shelter2 = 1 if wy==483 & st==27
* Mississippi (MS | 28) (wy 484)
replace shelter2 = 1 if date >= td(03apr2020) & st == 28
replace shelter2 = 1 if wy==484 & st==28
* Missouri (MO | 29) (wy 485)
replace shelter2 = 1 if date >= td(06apr2020) & st == 29
replace shelter2 = 1 if wy==485 & st==29
* Montana (MT | 30) (wy 483)
replace shelter2 = 1 if date >= td(28mar2020) & st == 30
replace shelter2 = 1 if wy==483 & st==30
* Nevada (NV | 32) (wy 484)
replace shelter2 = 1 if date >= td(31mar2020) & st == 32
replace shelter2 = 1 if wy==484 & st==32
* New Hampshire (NH | 33) (wy 483)
replace shelter2 = 1 if date >= td(28mar2020) & st == 33
replace shelter2 = 1 if wy==483 & st==33
* New Jersey (NJ | 34) (wy 482)
replace shelter2 = 1 if date >= td(21mar2020) & st == 34
replace shelter2 = 1 if wy==482 & st==34
* New Mexico (NM | 35) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 35
replace shelter2 = 1 if wy==483 & st==35
* New York (NY | 36) (wy 483)
replace shelter2 = 1 if date >= td(22mar2020) & st == 36
* NY exact start of the week
* North Carolina (NC | 37) (wy 484)
replace shelter2 = 1 if date >= td(30mar2020) & st == 37
replace shelter2 = 1 if wy==484 & st==37
* Ohio (OH | 39) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 39
replace shelter2 = 1 if wy==483 & st==39
* Oklahoma (OK | 40) (wy 484)
replace shelter2 = 1 if date >= td(01apr2020) & st == 40
replace shelter2 = 1 if wy==484 & st==40
* Oregon (OR | 41) (wy 483)
replace shelter2 = 1 if date >= td(23mar2020) & st == 41
replace shelter2 = 1 if wy==483 & st==41
* Pennsylvania (PA | 42) (wy 484)
replace shelter2 = 1 if date >= td(01apr2020) & st == 42
replace shelter2 = 1 if wy==484 & st==42
* Rhode Island (RI | 44) (wy 483)
replace shelter2 = 1 if date >= td(28mar2020) & st == 44
replace shelter2 = 1 if wy==483 & st==44
* South Carolina (SC | 45) (wy 485)
replace shelter2 = 1 if date >= td(07apr2020) & st == 45
replace shelter2 = 1 if wy==485 & st==45
* Tennessee (TN | 47) (wy 484)
replace shelter2 = 1 if date >= td(02apr2020) & st == 47
replace shelter2 = 1 if wy==484 & st==47
* Texas (TX | 48) (wy 484)
replace shelter2 = 1 if date >= td(02apr2020) & st == 48
replace shelter2 = 1 if wy==484 & st==48
* Vermont (VT | 50) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 50
replace shelter2 = 1 if wy==483 & st==50
* Virginia (VA | 51) (wy 484)
replace shelter2 = 1 if date >= td(30mar2020) & st == 51
replace shelter2 = 1 if wy==484 & st==51
* Washington (WA | 53) (wy 483)
replace shelter2 = 1 if date >= td(23mar2020) & st == 53
replace shelter2 = 1 if wy==483 & st==53
* West Virginia (WV | 54) (wy 483)
replace shelter2 = 1 if date >= td(24mar2020) & st == 54
replace shelter2 = 1 if wy==483 & st==54
* Wisconsin (WI | 55) (wy 483)
replace shelter2 = 1 if date >= td(25mar2020) & st == 55
replace shelter2 = 1 if wy==483 & st==55
	
* merge ACS data
gen fpy = string(fy, "%09.0f")
merge m:1 fpy using "acs1120_cn.dta", keep(master matched) nogen

gen month = ceil(wy/4)
replace year = 2012 if month==14
replace year = 2013 if month==27
replace year = 2014 if month==40
replace year = 2015 if month==53
replace year = 2016 if month==66
replace year = 2017 if month==79
replace year = 2018 if month==92
replace year = 2019 if month==105
replace year = 2020 if month==118

gen border_excl = 0
replace border_excl = 1 if fips==4001 | fips==20129 | fips==20187 | fips==20075 | fips==20071 | fips==20199 | fips==20181 | fips==20023 | fips==31057 | fips==31029 | fips==31135 | fips==31101 | fips==31049 | fips==31033 | fips==31105 | fips==35045 | fips==35039 | fips==35055 | fips==35007 | fips==35059 | fips==40025 | fips==49009 | fips==49047 | fips==49019 | fips==49037 | fips==56037 | fips==56007 | fips==56001 | fips==56021 | fips==16069 | fips==16057 | fips==16009 | fips==16055 | fips==16017 | fips==16021 | fips==24031 | fips==24033 | fips==51510 | fips==51013 | fips==51059 | fips==51610 | fips==6015 | fips==6093 | fips==6049| fips==16073 | fips==16027 | fips==16075 | fips==16087 | fips==16003 | fips==16049 | fips==4015 | fips==16083 | fips==16031 | fips==49003 | fips==49045 | fips==49023 | fips==49027 | fips==49001 | fips==49021 | fips==49053 | fips==4012 | fips==4027 | fips==09005 | fips==09003 | fips==09013 | fips==09015 | fips==33005 | fips==33011 | fips==33015 | fips==36083 | fips==36021 | fips==44007 | fips==44001 | fips==44005 | fips==50003 | fips==50025 | fips==18127 | fips==18091 | fips==18141 | fips==18039 | fips==18087 | fips==18151 | fips==39171 | fips==39051 | fips==39095 | fips==55003 | fips==55051 | fips==55125 | fips==55041 | fips==55037 | fips==55075 | fips==18089 | fips==18111 | fips==18007 | fips==18171 | fips==18165 | fips==18167 | fips==18153 | fips==18083 | fips==18129 | fips==19061 | fips==19097 | fips==19045 | fips==19163 | fips==19139 | fips==19115 | fips==19057 | fips==19111 | fips==21225 | fips==21055 | fips==21139 | fips==21145 | fips==21007 | fips==29045 | fips==29111 | fips==29127 | fips==29173 | fips==29163 | fips==29113 | fips==29183 | fips==29510 | fips==29189 | fips==29099 | fips==29186 | fips==29157 | fips==29031 | fips==29201 | fips==29133 | fips==55043 | fips==55065 | fips==55045 | fips==55105 | fips==55127 | fips==55059 | fips==33007 | fips==33003 | fips==33017

* ID neighboring states w/o RML
gen Eib = .
replace Eib = 158 if st==4 | st==20 | st==31 | st==35 | st==40 | st==49 | st==56
replace Eib = 185 if st==16 | st==41
replace Eib = 218 if st==24 | st==51
* NOTE: groups 249, 340 and 367 do not have any new bordering states that aren't already stated
replace Eib = 413 if st==9 | st==33 | st==36 | st==44 | st==50
replace Eib = 467 if st==18 | st==39 | st==55
replace Eib = 471 if st==19 | st==21 | st==29

gen g_idb = Eib
replace g_idb = 0 if g_idb==.

sum wy, meanonly
gen double t_centered = wy - r(mean)

levelsof st, local(state)
foreach s of local state {
	gen double trend_`s' = t_centered * (st==`s')
} // state trend loop

sum month, meanonly
gen double tm_centered = month - r(mean)
levelsof st, local(state)
foreach s of local state {
	gen double m_trend_`s' = tm_centered * (st==`s')
} // state trend loop

sum year, meanonly
gen double ty_centered = year - r(mean)
levelsof st, local(state)
foreach s of local state {
	gen double y_trend_`s' = ty_centered * (st==`s')
} // state trend loop

save "consumer_panel_hh.dta", replace


***** aggregate to month (4 week period) *****
gen month = ceil(wy/4)
drop if month==131

gen temp_postm = 0
replace temp_postm = 1 if post_start==1
egen postm = max(temp_postm), by(st month)

gen temp_startm = 0
replace temp_startm = 1 if start==1
egen startm = max(temp_startm), by(st month)
replace startm = 0 if startm==1 & postm==1

egen temp_g_idm = min(month) if postm==1, by(st)
replace temp_g_idm = 0 if temp_g_idm==.
egen g_idm = max(temp_g_idm), by(st)

collapse (sum) total_trip_spent junk_paid junk_new_paid snacks_paid chips_paid cookies_paid candy_paid icecream_paid soda_paid total_trips junk_trip junk_new_trip snacks_trip chips_trip cookies_trip candy_trip icecream_trip soda_trip quantity qjunk qjunk_new qsnacks qchips qcookies qcandy qicecream qsoda amt_oz amt_cnt amt_junk_oz amt_junk_cnt amt_junk_new_oz amt_junk_new_cnt amt_snacks_oz amt_snacks_cnt amt_chips_oz amt_chips_cnt amt_cookies_oz amt_cookies_cnt amt_candy_oz amt_candy_cnt amt_icecream_oz amt_icecream_cnt amt_soda_oz amt_soda_cnt (mean) postm startm g_idm junk_prop junk_new_prop snacks_prop chips_prop cookies_prop candy_prop icecream_prop soda_prop projection_factor household_income-panelist_zipcd fy date_week st, by(month fips household_cd)

drop if mod(g_idm,1) > 0
drop if mod(postm,1) > 0
drop if mod(startm,1) > 0
drop if mod(st,1) >0
*drop if g_idm==23 | g_idm==26 | g_idm==46 | g_idm==51 | g_idm==52 | g_idm==59 | g_idm==64 | g_idm==66 | g_idm==78

local paid total_trip_spent total_trips junk_paid junk_trip junk_new_paid junk_new_trip snacks_paid snacks_trip chips_paid chips_trip cookies_paid cookies_trip candy_paid candy_trip icecream_paid icecream_trip soda_paid soda_trip quantity qjunk qjunk_new qsnacks qchips qcookies qcandy qicecream qsoda amt_oz amt_cnt amt_junk_oz amt_junk_cnt amt_junk_new_oz amt_junk_new_cnt amt_snacks_oz amt_snacks_cnt amt_chips_oz amt_chips_cnt amt_cookies_oz amt_cookies_cnt amt_candy_oz amt_candy_cnt amt_icecream_oz amt_icecream_cnt amt_soda_oz amt_soda_cnt
foreach x of local paid {
	gen ln`x' = ln(`x' + 1)
} // ln loop
local trip junk junk_new snacks chips cookies candy icecream soda
foreach x of local trip {
	gen `x'_t = 0
	replace `x'_t = 1 if `x'_trip>0
} // trip loop

gen never = 0
replace never = 1 if g_idm==0

gen Eim = .
replace Eim = g_idm if g_idm!=0

* generate relative time to treatment (ttt)
gen ttt = month - Eim
tab ttt
forvalues k = 18(-1)2 {
	gen g_`k' = ttt == -`k'
} // lead loop
forvalues k = 0(1)36 {
	gen g`k' = ttt == `k'
} // lag loop
set matsize 1000

* merge ACS data
gen fpy = string(fy, "%09.0f")
merge m:1 fpy using "acs1120_cn.dta", keep(master matched) nogen

save "consumer_panel_hh_month.dta", replace


***** aggregate to quarter *****
use "consumer_panel_hh.dta", clear

gen quarter = ceil(wy/13)
drop if quarter==41

gen temp_postq = 0
replace temp_postq = 1 if post_start==1
egen postq = max(temp_postq), by(st quarter)

gen temp_startq = 0
replace temp_startq = 1 if start==1
egen startq = max(temp_startq), by(st quarter)
replace startq = 0 if startq==1 & postq==1

egen temp_g_idq = min(quarter) if postq==1, by(st)
replace temp_g_idq = 0 if temp_g_idq==.
egen g_idq = max(temp_g_idq), by(st)

collapse (sum) total_trip_spent junk_paid junk_new_paid snacks_paid chips_paid cookies_paid candy_paid icecream_paid soda_paid total_trips junk_trip junk_new_trip snacks_trip chips_trip cookies_trip candy_trip icecream_trip soda_trip quantity qjunk qjunk_new qsnacks qchips qcookies qcandy qicecream qsoda amt_oz amt_cnt amt_junk_oz amt_junk_cnt amt_junk_new_oz amt_junk_new_cnt amt_snacks_oz amt_snacks_cnt amt_chips_oz amt_chips_cnt amt_cookies_oz amt_cookies_cnt amt_candy_oz amt_candy_cnt amt_icecream_oz amt_icecream_cnt amt_soda_oz amt_soda_cnt (mean) shelter postq startq g_idq junk_prop junk_new_prop snacks_prop chips_prop cookies_prop candy_prop icecream_prop soda_prop projection_factor household_income-panelist_zipcd fy date_week st, by(quarter fips household_cd)

drop if mod(g_idq,1) > 0
drop if mod(postq,1) > 0
drop if mod(startq,1) > 0
drop if mod(st,1) >0
*drop if g_idq==4 | g_idq==28

replace shelter = 1 if shelter>0

local paid total_trip_spent total_trips junk_paid junk_trip junk_new_paid junk_new_trip snacks_paid snacks_trip chips_paid chips_trip cookies_paid cookies_trip candy_paid candy_trip icecream_paid icecream_trip soda_paid soda_trip quantity qjunk qjunk_new qsnacks qchips qcookies qcandy qicecream qsoda amt_oz amt_cnt amt_junk_oz amt_junk_cnt amt_junk_new_oz amt_junk_new_cnt amt_snacks_oz amt_snacks_cnt amt_chips_oz amt_chips_cnt amt_cookies_oz amt_cookies_cnt amt_candy_oz amt_candy_cnt amt_icecream_oz amt_icecream_cnt amt_soda_oz amt_soda_cnt
foreach x of local paid {
	gen ln`x' = ln(`x' + 1)
} // ln loop
local trip junk junk_new snacks chips cookies candy icecream soda
foreach x of local trip {
	gen `x'_t = 0
	replace `x'_t = 1 if `x'_trip>0
} // trip loop

gen never = 0
replace never = 1 if g_idq==0

gen Eiq = .
replace Eiq = g_idq if g_idq!=0

* generate relative time to treatment (ttt)
gen ttt = quarter - Eiq
tab ttt
forvalues k = 6(-1)2 {
	gen g_`k' = ttt == -`k'
} // lead loop
forvalues k = 0(1)12 {
	gen g`k' = ttt == `k'
} // lag loop
set matsize 800

* merge ACS data
gen fpy = string(fy, "%09.0f")
merge m:1 fpy using "acs1120_cn.dta", keep(master matched) nogen

sum quarter, meanonly
gen double t_centered = quarter - r(mean)

levelsof st, local(state)
foreach s of local state {
	gen double trend_`s' = t_centered * (st==`s')
} // state trend loop

save "consumer_panel_hh_quarter.dta", replace

***** End-of-File *****
